/*
 * @Author: your name
 * @Date: 2020-06-20 12:12:41
 * @LastEditTime: 2020-06-23 16:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \WWWe:\千峰线下\第二阶段\gulpfile.js
 */
const gulp = require("gulp");
const sass = require("gulp-sass");
const connect = require("gulp-connect");
const sourcemaps = require("gulp-sourcemaps");
const proxy = require('http-proxy-middleware');

// 搭建服务器
gulp.task("server", done => {

  connect.server({
    root: "dist",

    host: '192.168.17.33',
    port: 8080,
    livereload: true,
    open: true

  })

  done();
})
// gulp.task("server", done => {

//   connect.server({
//     root: "dist",

//     livereload: true
//   })

//   done();
// })

// gulp.task('server', function () {
//   connect.server({
//     root: "dist",　//代理的项目的目录
//     port: 9002,　　//端口号
//     host: '::',　　//之前没加这个不能让手机访问，加上这个就可以了
//     livereload: false, //不实时修改联动
//     middleware: function (connect, opt) {
//       return [
//         proxy('/api', {
//           target: 'http://47.99.43.186',
//           //target: 'http://demo3.renrunkeji.com:8194',
//           // pathRewrite: {
//           //   '^/api': '/api'
//           // },
//           // changeOrigin: true,
//           // prependPath: false
//         })
//       ]
//     }
//   });
// });

// html文件
gulp.task("copyHtml", done => {

  gulp.src("*.html").pipe(
    gulp.dest("dist")
  ).pipe(connect.reload());

  done();
})

gulp.task("copyHtmls", done => {

  gulp.src("./pages/*.html").pipe(
    gulp.dest("./dist/pages")
  ).pipe(connect.reload());

  done();
})
// 监听img
gulp.task("copyImg", done => {

  gulp.src("./img/*.png").pipe(
    gulp.dest("./dist/img")
  ).pipe(connect.reload());

  done();
})
// 监听js文件
gulp.task("copyJs", done => {

  gulp.src("./JavaScript/*.js").pipe(
    gulp.dest("./dist/JavaScript")
  ).pipe(connect.reload());

  done();
})
// sass转css文件
gulp.task("sass", (done) => {
  gulp.src("./css/*.scss")
    .pipe(sourcemaps.init())
    //nested
    //expanded
    //compact
    //compressed
    .pipe(sass({
      outputStyle: "compressed"
    }))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest("dist/css"));

  done();
});

gulp.task("sassa", done => {

  gulp.src("./css/*.scss").pipe(
    gulp.dest("dist/css")
  ).pipe(connect.reload());

  done();
})
/* css文件 */
gulp.task("css", done => {

  gulp
    .src("./css/*.css")
    .pipe(connect.reload())
    .pipe(sass({
      outputStyle: "compressed"
    }))
    .pipe(sourcemaps.write())
    .pipe(
      gulp.dest("./dist/css")
    );

  done();
})
// 文件监听
gulp.task("watch", (done) => {
  gulp.watch("./css/*.scss", gulp.series("sass"));
  gulp.watch("*.html", gulp.series("copyHtml"));
  gulp.watch("./pages/*.html", gulp.series("copyHtmls"));
  gulp.watch("./JavaScript/*.js", gulp.series("copyJs"));
  gulp.watch("./css/*.css", gulp.series("css"));
  gulp.watch("./css/*.scss", gulp.series("sassa"));
  gulp.watch("./img/*.png", gulp.series("copyImg"));

  done();
});

gulp.task("default", gulp.series("server", "watch"));